﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

using Object = UnityEngine.Object;

namespace PP.Basic
{
    public static class AudioObjectExtension
    {
        /// <summary>
        /// 创建一个新对象和音频组件用于播放音频，只缓存音频片段，不缓存音频对象，由外部维护音频对象的生命周期
        /// </summary>
        /// <param name="o">扩展对象</param>
        /// <param name="audioPath">Addressable路径</param>
        /// <param name="isPlayNow">是否立即播放</param>
        /// <param name="isLoop">是否循环</param>
        /// <returns>AudioSouce音频播放组件，由调用者维护</returns>
        public static AudioSource CreateAudio(this Object o, string audioPath, bool isPlayNow = true, bool isLoop = false)
        {
            var co = Audio.Instance.CreateAudio(audioPath, isPlayNow, isLoop);
            return co;
        }

        /// <summary>
        /// 播放音效路径的音频资源
        /// </summary>
        /// <param name="o">扩展对象</param>
        /// <param name="audioPath">音频资源路径</param>
        /// <param name="isLoop">是否循环播放</param>
        /// <param name="autoReleaseAudioClip">
        /// true, 则在音效播放完后，自动是否资源；
        /// false, 则需要自行调用 <see cref="ReleaseAudio(AudioSource)"/> 方法来释放资源；
        /// isLoop: true 时，autoReleaseAudioClip 无效，需要调用 <see cref="ReleaseAudio(Transform, AudioSource)"/> 方法来释放资源；
        /// </param>
        /// <returns>AudioPlayTask，可以用来控制播放</returns>
        public static AudioPlayTask PlayAudio(this Object o, string audioPath, bool isLoop = false, bool autoReleaseAudioClip = false, Action<AudioPlayTask> onComplete = null, bool forcePlay = false)
        {
            var playTask = Audio.Instance.PlayAudio(audioPath, isLoop, autoReleaseAudioClip, onComplete, o, forcePlay);
            return playTask;
        }

        /// <summary>
        /// 播放音频片段
        /// </summary>
        /// <param name="o">扩展对象</param>
        /// <param name="audioClip">音效片段</param>
        /// <param name="isLoop">是否循环播放</param>
        /// <param name="autoReleaseAudioClip">
        /// true, 则在音效播放完后，自动是否资源；
        /// false, 则需要自行调用 <see cref="ReleaseAudio(AudioSource)"/> 方法来释放资源；
        /// isLoop: true 时，autoReleaseAudioClip 无效，需要调用 <see cref="ReleaseAudio(Transform, AudioSource)"/> 方法来释放资源；
        /// </param>
        /// <returns>AudioPlayTask，可以用来控制播放</returns>
        public static AudioPlayTask PlayAudio(this Object o, AudioClip audioClip, bool isLoop = false, bool autoReleaseAudioClip = false, Action<AudioPlayTask> onComplete = null, bool forcePlay = false)
        {
            var playTask = Audio.Instance.PlayAudio(audioClip, isLoop, autoReleaseAudioClip, onComplete, o, forcePlay);
            return playTask;
        }

        /// <summary>
        /// 释放音效资源
        /// </summary>
        public static void ReleaseAudio(this Object o, string audioPath)
        {
            Audio.Instance.ReleaseAudio(audioPath);
        }

        /// <summary>
        /// 释放音效资源
        /// </summary>
        public static void ReleaseAudio(this Object o, AudioClip audioClip)
        {
            Audio.Instance.ReleaseAudio(audioClip);
        }

        public static void PlayVibrate(this Object o, float duration = 0.4f)
        {
            Audio.Instance.PlayVibrate(duration);
        }

        public static bool IsPlayedToEnd(this AudioSource co, float tolerance = 0.01f)
        {
            return Mathf.Abs(co.clip.length - co.time) <= tolerance;
        }
    }
}
